package com.ruicar.afs.cloud.afscase.dispatch.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelAuthorizeRegion;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelBaseInfo;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelAuthorizeRegionService;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelBaseInfoService;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelRiskInfoService;
import com.ruicar.afs.cloud.afscase.dispatch.DispatchContext;
import com.ruicar.afs.cloud.afscase.dispatch.annotation.Dispatch;
import com.ruicar.afs.cloud.afscase.dispatch.enums.DispatchTypeEnum;
import com.ruicar.afs.cloud.afscase.dispatch.service.DispatchBaseServcie;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseBaseInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseCarInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseChannelInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseContractInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.service.*;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cheng.bi
 * 置顶处理
 */
@Service
@Dispatch(name = "放款置顶", group = DispatchTypeEnum.LOAN)
public class DispatchTopLoanService extends DispatchBaseServcie {

    @Autowired
    private CaseContractInfoService contractInfoService;
    @Autowired
    private CaseBaseInfoService caseBaseInfoService;
    @Autowired
    private CaseChannelInfoService infoService;
    @Autowired
    private ChannelRiskInfoService channelRiskInfoService;
    @Autowired
    private CaseCarStyleDetailService carStyleDetailInfoService;
    @Autowired
    private CaseCarInfoService caseCarInfoService;
    @Autowired
    private ChannelBaseInfoService channelBaseInfoService;
    @Autowired
    private ChannelAuthorizeRegionService regionService;

    /**
     * 分单前实际处理方法
     *
     * @param context
     */
    public void process(DispatchContext context) {
        List<String> applyStatusList = new ArrayList<>();
        applyStatusList.add(ApplyStatusEnum.LOAN_QUEUE.getState());
        applyStatusList.add(ApplyStatusEnum.WAIT_ASSIGN.getState());
        List<CaseContractInfo> contractList = contractInfoService.list(Wrappers.<CaseContractInfo>lambdaQuery()
                .in(CaseContractInfo::getApplyStatus, applyStatusList)
                /**自动审核不完成不允许自动发起流程*/
                .eq(CaseContractInfo::getWorkflowPrevStep,100));
        context.put(LOAN_KEY, contractList);
        if (contractList != null && !contractList.isEmpty()) {
            contractList.stream().filter(info -> {
                CaseBaseInfo baseInfo = caseBaseInfoService.getOne(Wrappers.<CaseBaseInfo>lambdaQuery().eq(CaseBaseInfo::getApplyNo, info.getApplyNo()));
                JSONObject jsonObject = new JSONObject();
                /**业务类型*/
                jsonObject.put("businessType", baseInfo.getBusinessType());
                /**车辆类型*/
                jsonObject.put("carType", baseInfo.getCarType());
                /**车辆属性*/
                jsonObject.put("carNature", baseInfo.getCarNature());
                /**运营方式*/
                jsonObject.put("operateWay", baseInfo.getOperateWay());
                /**挂靠方式*/
                jsonObject.put("affiliatedWay", baseInfo.getAffiliatedWay());
                /**贷款总额*/
                jsonObject.put("loanAmount", baseInfo.getLoanAmtRepeat());
                /**金融产品*/
                jsonObject.put("productId", String.valueOf(baseInfo.getProductId()));
                CaseChannelInfo caseChannelInfo = infoService.getOne(Wrappers.<CaseChannelInfo>query().lambda().eq(CaseChannelInfo::getApplyNo, info.getApplyNo()));
                if (ObjectUtil.isNotNull(caseChannelInfo)) {
                    /**经销商名称*/
                    jsonObject.put("dealerName", caseChannelInfo.getDealerName());
                    ChannelBaseInfo channelBaseInfo = channelBaseInfoService.getOne(Wrappers.<ChannelBaseInfo>query().lambda().eq(ChannelBaseInfo::getChannelCode, caseChannelInfo.getDealerNo()));
                    if (ObjectUtil.isNotNull(channelBaseInfo)) {
                        /**经销商省份*/
                        jsonObject.put("channelProvince", channelBaseInfo.getChannelProvince());
                        /**经销商城市*/
                        jsonObject.put("channelCity", channelBaseInfo.getChannelCity());
                        /**渠道归属*/
                        jsonObject.put("channelBelong", channelBaseInfo.getChannelBelong());
                        List<ChannelAuthorizeRegion> list = regionService.list(Wrappers.<ChannelAuthorizeRegion>query().lambda().eq(ChannelAuthorizeRegion::getChannelId, channelBaseInfo.getChannelId()));
                        if (list != null && !list.isEmpty()) {
                            String practicesProvince = "";
                            String practicesCity = "";
                            for (ChannelAuthorizeRegion region : list) {
                                if (IsAssignedEnum.YES.getCode().equals(region.getParentId()) && IsAssignedEnum.YES.getCode().equals(region.getIsParent())) {
                                    practicesProvince = practicesProvince + region.getCode() + ",";
                                }
                                if (IsAssignedEnum.NO.getCode().equals(region.getIsParent())) {
                                    practicesCity = practicesCity + region.getCode() + ",";
                                }
                            }
                            /**展业省份*/
                            jsonObject.put("practicesProvince", practicesProvince.substring(0, practicesProvince.length() - 1));
                            /**展业城市*/
                            jsonObject.put("practicesCity", practicesCity.substring(0, practicesCity.length() - 1));
                        }
                    }
                }
                CaseCarInfo caseCarInfo = caseCarInfoService.getOne(Wrappers.<CaseCarInfo>query().lambda().eq(CaseCarInfo::getApplyNo, info.getApplyNo()));
                if (ObjectUtil.isNotNull(caseCarInfo)) {
                    /**车辆级别*/
                    jsonObject.put("carBodyClass", caseCarInfo.getCarBodyClass());
                    /**车型*/
                    jsonObject.put("modelCode", caseCarInfo.getModelCode());
                    /**购车地省份*/
                    jsonObject.put("purchaseProvince", caseCarInfo.getPurchaseCity());
                    /**购车地城市*/
                    jsonObject.put("purchaseCity", caseCarInfo.getPurchaseCity());
                    /**上牌地省份*/
                    jsonObject.put("licenseProvince", caseCarInfo.getLicenseProvince());
                    /**上牌地城市*/
                    jsonObject.put("licenseCity", caseCarInfo.getLicenseCity());
                }
                /**是否网约车*/
                if (StringUtil.isNotEmpty(baseInfo.getAffiliatedWay()) && AffiliatedWayEnum.NETWORK_CAR_AFFILIATED.getCode().equals(baseInfo.getAffiliatedWay())) {
                    jsonObject.put("onlineCar", WhetherEnum.YES.getCode());
                } else {
                    jsonObject.put("onlineCar", WhetherEnum.NO.getCode());
                }
                /**是否归档超期/是否暂停放款*/
                if(WhetherEnum.YES.getCode().equals(info.getIsOverdue())){
                    jsonObject.put("isOverdue", WhetherEnum.YES.getCode());
                }else {
                    jsonObject.put("isOverdue", WhetherEnum.NO.getCode());
                }
                if(WhetherEnum.YES.getCode().equals(info.getIsLock())){
                    jsonObject.put("isLock", WhetherEnum.YES.getCode());
                }else {
                    jsonObject.put("isLock", WhetherEnum.NO.getCode());
                }

                context.put(info.getId(), jsonObject);
                return true;
            }).collect(Collectors.toList());
        }
    }
}
